const prepared = require('./prepare').prepare()

if (!prepared) {
    return
}

const logger = require('./utils/console-logger')
const baseConfig = require('./base-config')

const Koa = require('koa')
const koaBody = require('koa-body')

const accessLogger  = require('./middlewares/access-logger')
const serve = require('koa-static');
const authCheck  = require('./middlewares/auth-check')
const cors = require('./middlewares/cors')
const errorCatch  = require('./middlewares/error-catch')
const routes = require('./routes')



const app = new Koa()


// 访问 logs
app.use(accessLogger())
// web页面
app.use(serve(baseConfig.webui))
// 跨域
app.use(cors())
// 异常捕获
app.use(errorCatch())
// 权限检查
// app.use(authCheck())
// 支持文件上传
app.use(koaBody({
    multipart: true,
    formidable: {
        maxFileSize: 10*1024*1024    // 设置上传文件大小最大限制，默认2M
    }
}))
// 使用ctx.body解析中间件
// app.use(koaBodyParser())
// 装载所有业务子路由
app.use(routes.routes())
// 加载路由中间件
app.use(routes.routes()).use(routes.allowedMethods())

app.listen(baseConfig.port)

logger.log('App is starting at port ', baseConfig.port)
// print banner
require('fs').readFile(__dirname + '/banner.txt', 'utf8', (error, data) => {
    logger.log(data)
    logger.log(`:: Control :: (v${baseConfig.version})`)
})